package com.bicycle.rentalsystem.servelt.admin;

import com.bicycle.rentalsystem.pojo.admin.Bicycle;
import com.bicycle.rentalsystem.pojo.admin.Page;
import com.bicycle.rentalsystem.pojo.admin.Station;
import com.bicycle.rentalsystem.service.admin.BicycleService;
import com.bicycle.rentalsystem.service.admin.StationService;
import com.bicycle.rentalsystem.service.impl.admin.BicycleServiceImpl;
import com.bicycle.rentalsystem.service.impl.admin.StationServiceImpl;
import com.bicycle.rentalsystem.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Third uncle
 * @date 2022/11/23 - 20:10
 */
@WebServlet("/bikeServlet")
public class BikeServlet extends BaseServlet{
    BicycleService bicycleService=new BicycleServiceImpl();
    StationService stationService=new StationServiceImpl();
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.查询全部自行车
        List<Bicycle> list = bicycleService.searchBicycles();
        //2.自行车保存到request域中
        req.setAttribute("list",list);
        //3.请求转发
        req.getRequestDispatcher("admin/a4.jsp").forward(req,resp);

    }
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String pageNo = req.getParameter("pageNo");
        /*if(pageNo==null||pageNo==""){
            pageNo="1";
        }
        Integer pageNo2= Integer.valueOf(pageNo);*/
        int pageNo2 = WebUtil.parseInt(pageNo, 1);
        int pageSize = WebUtil.parseInt(req.getParameter("pageSize"), 4);
        //获取page对象
        Page<Bicycle> page = bicycleService.page(pageNo2, 3);
        //保存到request域中
        req.setAttribute("page",page);

        //查询以租赁有多少
        int i = bicycleService.queryStatus();
        req.setAttribute("number",i);

        //请求转发
        req.getRequestDispatcher("admin/a4.jsp").forward(req,resp);

    }
    protected void page2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String pageNo = req.getParameter("pageNo");
        /*if(pageNo==null||pageNo==""){
            pageNo="1";
        }
        Integer pageNo2= Integer.valueOf(pageNo);*/
        int pageNo2 = WebUtil.parseInt(pageNo, 1);
        int pageSize = WebUtil.parseInt(req.getParameter("pageSize"), 2);
        //获取page对象
        Page<Bicycle> page = bicycleService.page(pageNo2, pageSize);
        //保存到request域中
        req.setAttribute("page",page);
        //请求转发
        req.getRequestDispatcher("admin/a.jsp").forward(req,resp);

    }
    protected void getBike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取站点
        List<Station> list = stationService.getStation();
        //保存到reqest域中
        req.setAttribute("list",list);
        //转发到添加页面
        req.getRequestDispatcher("admin/addBike.jsp").forward(req, resp);


    }
    protected void getBike2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取站点
        List<Station> list = stationService.getStation();
        //保存到reqest域中
        req.setAttribute("list",list);
        //获取参数值
        String id = req.getParameter("id");
        Integer bid= Integer.valueOf(id);
        //获取bike
        Bicycle bike = bicycleService.getBikeById(bid);
        //保存到reqest域中
        req.setAttribute("bike",bike);
        //转发到添加页面
        req.getRequestDispatcher("admin/updateBike.jsp").forward(req, resp);


    }
    protected void addBike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String number = req.getParameter("bianhao");
        String type = req.getParameter("leixing");
        String bname = req.getParameter("pingpai");
        String rent = req.getParameter("zujing");
        String stationName = req.getParameter("zulinzhandian");
        //保存到数据库
        Bicycle bike = new Bicycle(null, number, type, bname, rent, "未租赁", null,stationName);
        bicycleService.savaBike(bike);
        //重定向回去
        resp.sendRedirect(req.getContextPath()+"/bikeServlet?action=page2");

    }
    protected void updateBike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id = req.getParameter("id");
        Integer bid= Integer.valueOf(id);
        String number = req.getParameter("bianhao");
        String type = req.getParameter("leixing");
        String bname = req.getParameter("pingpai");
        String rent = req.getParameter("zujing");
        String status = req.getParameter("zhuangtai");
        String stationName = req.getParameter("zulinzhandian");
        //数据库中修改
        Bicycle bike = new Bicycle(bid, number, type, bname, rent, status, null, stationName);
        bicycleService.updateBike(bike);
        //重定向回去
        resp.sendRedirect(req.getContextPath()+"/bikeServlet?action=page2");

    }
    protected void deleteBike(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id = req.getParameter("id");
        Integer bid= Integer.valueOf(id);
        //根据id删除bike
        bicycleService.deleteBikeById(bid);
        //重定向回去
        resp.sendRedirect(req.getContextPath()+"/bikeServlet?action=page2");

    }



}
